package com.aihna.mall.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.mall.domain.bo.StoreUserLevelBo;
import com.aihna.mall.domain.vo.StoreUserLevelVo;
import com.aihna.mall.domain.StoreUserLevel;
import com.aihna.mall.mapper.StoreUserLevelMapper;
import com.aihna.mall.service.IStoreUserLevelService;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户等级配置Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-04-14
 */
@RequiredArgsConstructor
@Service
public class StoreUserLevelServiceImpl implements IStoreUserLevelService {

    private final StoreUserLevelMapper baseMapper;

    /**
     * 查询用户等级配置
     */
    @Override
    public StoreUserLevelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户等级配置列表
     */
    @Override
    public TableDataInfo<StoreUserLevelVo> queryPageList(StoreUserLevelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreUserLevel> lqw = buildQueryWrapper(bo);
        Page<StoreUserLevelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户等级配置列表
     */
    @Override
    public List<StoreUserLevelVo> queryList(StoreUserLevelBo bo) {
        LambdaQueryWrapper<StoreUserLevel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreUserLevel> buildQueryWrapper(StoreUserLevelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreUserLevel> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增用户等级配置
     */
    @Override
    public Boolean insertByBo(StoreUserLevelBo bo) {
        StoreUserLevel add = MapstructUtils.convert(bo, StoreUserLevel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户等级配置
     */
    @Override
    public Boolean updateByBo(StoreUserLevelBo bo) {
        StoreUserLevel update = MapstructUtils.convert(bo, StoreUserLevel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StoreUserLevel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户等级配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
